<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use App\Models\UploadFile;
use App\Models\Grid;
use App\Models\GridArea;
use App\Models\GridImg;
use App\Models\GridAssess;
use App\Models\GridPrice;
use App\Models\GridHouse;
use App\Models\Villager;
use App\Models\SignSet;
use App\Models\ProtocolSet;
use App\Models\PrintLog;
use App\Models\SignSetType;
use App\Models\User;
use App\Models\ApprovalSet;
use App\Models\ApprovalLog;
use App\Models\ApprovalList;
use App\Models\BuildForm;
use App\Models\Street;
use App\Models\Village;
use App\Models\ProtocolPlan;
use App\Services\UploadService;


class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    // 根据当前登录的项目身份展示对应的内容
    public function getAppWhere()
    {
        $project_id = $_GET['project_id'] ?? '';
        if (!$project_id) {
            $project_id = auth('admin')->user()->project_id;
        }

        return ['project_id' => $project_id];
    }

    // // 根据当前登录的项目身份展示对应的内容
    // public function getAppWhere(){
    //     return [];
    // }

    /**
     * 根据图片id获取图片
     * file_id:图片id
     * return_type:0单图时返回一维数组1单图时返回二维
     */
    public function getImageUrl($file_id, $return_type = 0)
    {
        if (strpos($file_id, ',')) {
            $file_arr = explode(',', $file_id);
            $data = [];
            foreach ($file_arr as $v) {
                $li = UploadService::init()->getFileIdToRemoteUrl($v);
                $data[] = $li;
            }
        } else {
            if ($return_type) {
                $data = UploadService::init()->getFileIdToRemoteUrl($file_id);
                $data = [$data];
            } else {
                $data = UploadService::init()->getFileIdToRemoteUrl($file_id);
            }
        }
        return $data;
    }

    // 根据当前入户是否全部完成设置入户状态
    public function setRuhuStatus($grid_id, $str)
    {
        $update = Grid::query()->where('id', $grid_id)->update([$str => 1]);
        $find = Grid::query()->where('id', $grid_id)->first();
        $li['idcard_status'] = $find['idcard_status'];
        $li['hukou_status'] = $find['hukou_status'];
        $li['fangchan_status'] = $find['fangchan_status'];
        $li['area_status'] = $find['area_status'];
        $flag = true;
        foreach($li as $v) {
            if ($v != 1) {
                $flag = false;
                break;
            }
        }
        if ($flag) {
            $update = Grid::query()->where('id', $grid_id)->update(['into_door_status' => 1]);
            
            if(!isset($params['table_type'])){//正常环节提交(非拒绝后)
                if($find['if_mapping']==2&&$find['dualuse_area']==0){  
                    // 触发审批-无异议表
                    if(!$this->findApproval($grid_id, 14)){
                        $res = $this->setApproval($grid_id, 14);
                    }  
                }
            }
        }
    }

    // 查询当前状态是否需要生成新的表审批
    public function findApproval($grid_id, $template_id)
    {
        $apply_status=ApprovalList::query()
             ->where('grid_id',$grid_id)
             ->where('apply_type',$template_id)
             ->orderByDesc('id')
             ->value('apply_status');
        if($apply_status===null){
            return false;
        }else{
            if(in_array($apply_status,[0,1,3])){
                return true;
            }else{
                return false;
            }
        }     
       
    }

    // 新增审批
    public function setApproval($grid_id, $template_id)
    {
        $grid=Grid::query()->select('project_id','street_id','village_id')->where('id',$grid_id)->first();

        $approval_set = ApprovalSet::query()
            ->where('project_id', $grid['project_id'] ?? 0)
            ->where('street_id', $grid['street_id'] ?? 0)
            ->where('village_id', $grid['village_id'] ?? 0)
            ->where('template_id', $template_id)
            ->where('status', 1)
            ->first();
        if (!$approval_set) {
            return showError('请先在后台配置审批模板');
        }

        $data['user_id'] = auth('api')->user()->id ?? 0;
        $data['project_id'] = $grid['project_id'] ?? 0;
        $data['apply_type'] = $template_id;
        $data['apply_reason'] = '';
        $data['content'] = '';
        $data['grid_id'] = $grid_id;
        $data['set_id']= $approval_set['id'];

        $res = ApprovalList::query()->create($data);

        $content = json_decode($approval_set['content'], true);
        foreach ($content as $k => $v) {
            $user_arr=explode(',',$v['user_ids']);
            foreach($user_arr as $vv){
                $logData['list_id']=$res->id;
                $logData['apply_user_id']=$vv;
                $logData['step']=$k+1;
                $approval_set=ApprovalLog::query()
                        ->create($logData);
            }
        }
    }

    // 根据网格id获取该使用面积确认数据还是测绘数据
    public function getAreaType($grid_id)
    {
        $mapping_company_id = Grid::query()->where('id', $grid_id)->value('mapping_company_id');
        $area_table = GridArea::query()->where('grid_id', $grid_id)->where('type', 1)->value('id');
        $area_assess = GridArea::query()->where('grid_id', $grid_id)->where('type', 2)->value('id');
        if ($mapping_company_id && !$area_assess) {
            return showError('未进行测绘，无法操作');
        }
        if (!$mapping_company_id && !$area_table) {
            return showError('未填写面积确认表，无法操作');
        }

        if ($mapping_company_id) {
            $type = 2;
        } else {
            $type = 1;
        }

        return $type;
    }

    // 获取打印数据
    public function getProtocolData($params)
    {
        $sign_set_type = SignSetType::query()->where('id', $params['sign_set_type'])->first();
        $price_type=0;
        if ($sign_set_type['type'] == 1) { //签约
            $find1 = ApprovalList::query()
            ->where('grid_id', $params['grid_id'])
            ->whereIn('apply_type', [14,15,17,18])
            ->orderByDesc('id')
            ->first();
            if ($find1) {
              if ($find1['apply_status']!=1) {
                return showError('审批通过后才可打印');
              }
            }
            $find2 = ApprovalList::query()
            ->where('grid_id', $params['grid_id'])
            ->where('apply_type', 16)
            ->orderByDesc('id')
            ->first();
            if ($find2) {
                if ($find2['apply_status']!=1) {
                  return showError('审批通过后才可打印');
                }
            }

            // 录入补偿款数据
            $price_type_arr=[1=>4,2=>0];
            $price_type=$price_type_arr[$params['type']];
            $set_tally = $this->setTally($params['grid_id'],$price_type);
            if (isset($set_tally['code']) && $set_tally['code'] == 0) {
                return $set_tally;
            }
        } else {
            $set_tally = [];
        }

        if ($sign_set_type['template_id']) { //是否需要审批
            $find_approval = ApprovalList::query()
                ->where('grid_id', $params['grid_id'])
                ->where('apply_type', $sign_set_type['template_id'])
                ->orderByDesc('id')
                ->first();
            if ($find_approval) {
                if ($find_approval['apply_status'] != 1) {
                    return showError('审批通过后才可打印');
                }
            } else {
                return showError('审批通过后才可打印');
            }
        } else {
            $find_approval = [];
        }
        //查询打印数据
        $type = $this->getAreaType($params['grid_id']);
        if ($type != 1 && $type != 2) {
            return $type;
        }
        
        $grid = Grid::query()
            ->with('villager')
            ->with('agent')
            ->with(['grid_area' => function ($query) use ($type) {
                $query->where('type', $type);
            }])
            ->with(['grid_house' => function ($query) {
                $query->where('type', 1)
                    ->with('house_type:id,name,area');
            }])
            ->with('grid_img')
            ->with(['grid_price' => function ($query) use ($price_type) {
                $query->where('type', $price_type??0);
            }])
            ->where('id', $params['grid_id'])
            ->first()
            ->toArray();
          
        foreach ($grid['grid_img'] as &$v) {
            $li[$v['type']][] = $v;
        }
        $grid['grid_img_arr'] = $li ?? [];
        unset($grid['grid_img']);
        // var_dump(strstr($sign_set_type['match_level'],"2"));exit;

        //查询打印模板
        $data = SignSet::query()
            ->when(strstr($sign_set_type['match_level'],"1"), function ($query) use ($grid) {
                $query->where('build_form', $grid['build_form']);
            })
            ->when(strstr($sign_set_type['match_level'],"2"), function ($query) use ($grid) {
                $query->where('supply_form', $grid['supply_form']);
            })
            ->when(strstr($sign_set_type['match_level'],"3"), function ($query) use ($grid) {
                $query->where('village_id', $grid['village_id']);
            })
            ->where('project_id', $grid['project_id'])
            ->where('sign_set_type', $params['sign_set_type'])
            ->first();
        if (!$data) {
            return showError('请先在后台配置协议模板');
        }
        $getContent = $this->getContent($grid, $set_tally, $data, $params, $find_approval);
        if (isset($getContent['code']) && $getContent['code'] == 0) {
            return $getContent;
        }
        $data['content'] = $getContent['content'];
        //额外打印数据
        $data['supply_form'] = $grid['supply_form'];
        $data['build_form'] = $grid['build_form'];
        $data['auto_num'] = $getContent['auto_num'];
        $data['number_header'] = $data['number_header'].$data['auto_num'];

        // $path = public_path();
        $name = rand(10000, 99999) . time();
        $pdf_url = 'pdf/' . $name  . '.pdf';
        $html_url = 'html/' . $name  . '.html';
        file_put_contents($html_url, $data['content']);

        shell_exec(env('WPDF') . " " . $html_url . " " . $pdf_url);
        $data['pdf_url'] = env('HTTP_URL') . '/' . $pdf_url;

        $res = $data;

        return $res;
    }

    // 录入补偿款数据
    // grid_id 网格id
    // price_type 4查看0打印
    public function setTally($grid_id,$price_type=4)
    {
        if($price_type!=4&&$price_type!=0){
            return showError('price_type参数类型有误');
        }

        $grid = Grid::query()->where('id', $grid_id)->first();

        if ($grid['into_door_status'] != 1) {
            return showError('请先完成入户');
        }
       
        $set = ProtocolSet::query()
            // ->select('id','require_clear_start_time','require_clear_end_time')
            ->where('supply_form', $grid['supply_form'])
            ->where('build_form', $grid['build_form'])
            ->where('street_id', $grid['street_id'])
            ->where('village_id', $grid['village_id'])
            ->where('project_id', $grid['project_id'])
            ->where('segment_id', $grid['segment_id'])
            ->first();


        if (empty($set)) {
            return showError('未查询到配置信息，请联系管理员到后台配置');
        }

        $type = $this->getAreaType($grid_id);
        if ($type != 1 && $type != 2) {
            return $type;
        }
        $grid_area = GridArea::query()->where('grid_id', $grid_id)->where('type', $type)->first(); //面积表


        $grid_assess = GridAssess::query()->where('grid_id', $grid_id)->first(); //评估数据
        if ($grid['assess_company_id'] != '' && !$grid_assess) {
            return showError('请先进行评估');
        }

        if ($grid['supply_form'] == 2) { //房屋
            $grid_house = GridHouse::query()
                ->select('house_type_id', 'is_decorate')
                ->with('house_type:area,id')
                ->where('grid_id', $grid_id)
                ->where('type', 1)
                ->get()->toArray();
            if (!$grid_house) {
                return showError('请先选择户型');
            }

            //简装/不简装补助费
            $if_decorate_fee = 0;
            $total_area = 0;
            foreach ($grid_house as $v) {
                if ($v['is_decorate'] == 1) {
                    $if_decorate_fee += $set['decorate_price'] * $v['house_type']['area'];
                } else {
                    $if_decorate_fee += $set['no_decorate_price'] * $v['house_type']['area'];
                }
                $total_area += $v['house_type']['area']; //选择的总面积
            }

            $difference_area = round($grid_area['supply_area'] - $total_area,2); //面积差值（上靠（E=负数）/下靠（E=正数））
            //房屋上下靠结算差额
            $house_difference_fee = $difference_area * $set['money_price'];
            //房屋上下靠结算差额的绝对值
            // $data['house_difference_fee']=abs($house_difference_fee);
        } else {
            //货币补偿金
            $money_fee = $set['money_price'] * $grid_area['supply_area'];

        }

        // 搬迁补助费
        $subsidy_fee = $set['subsidy_fee'];

        $clear_time = $sign_time = date('Y-m-d');
        // 规定时间签约奖励费
        $require_sign_fee = $set['require_sign_start_time'] < $sign_time && $set['require_sign_end_time'] > $sign_time ? $set['require_sign_fee'] : 0;
        // 规定时间腾房奖励费
        $require_clear_fee = $set['require_clear_start_time'] < $clear_time && $set['require_clear_end_time'] > $clear_time ? $set['require_clear_fee'] : 0;
        //停产停业损失补偿金
        $stop_fee = $grid_area['dualuse_area'] * $set['stop_price'] * $set['stop_month'];

        $type = $grid['supply_form'] . ',' . $grid['build_form'];
        switch ($type) {
            case '2,1': //房屋-带院住宅
                // 搬迁补助费
                $data['subsidy_fee'] = $subsidy_fee;
                // 临时过渡费
                $transition_fee = $set['transition_price'] * $set['transition_month'] * ($grid_area['use_area'] + $grid_area['two_area']);
                $data['transition_fee'] = $transition_fee;
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $stop_fee;
                //简装/不简装补助费
                $data['if_decorate_fee'] = $if_decorate_fee;
                //房屋上下靠结算差额
                $data['house_difference_fee'] = $house_difference_fee;
                 // 奖励临时过渡费
                 $data['other_subsidy_fee']=$other_subsidy_fee = $set['transition_start_time'] < $sign_time && $set['transition_end_time'] > $sign_time ? $set['reward_transition_price'] * $set['transition_month'] * ($grid_area['use_area'] + $grid_area['two_area']): 0;
                //总计补助费
                $data['supply_total_fee'] = $supply_total_fee = $subsidy_fee + $transition_fee
                    + $require_sign_fee + $require_clear_fee + $if_decorate_fee + $stop_fee+$other_subsidy_fee;

                $party_fee = $supply_total_fee + $house_difference_fee; //相互结算总账款
                if ($party_fee > 0) {
                    $party_a_fee = abs($party_fee);
                } else {
                    $party_b_fee = abs($party_fee);
                }
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $party_a_fee ?? 0;
                //被征收人应支付征收部门费用
                $data['party_b_fee'] = $party_b_fee ?? 0;
                break;
            case '2,2': //房屋-多层楼房
                // 搬迁补助费
                $data['subsidy_fee'] = $subsidy_fee;
                // 临时过渡费
                $transition_fee = $set['transition_price'] * $set['transition_month'] * ($grid_area['build_area'] + $grid_area['improve_area']);
                $data['transition_fee'] = $transition_fee;
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $stop_fee;
                //简装/不简装补助费
                $data['if_decorate_fee'] = $if_decorate_fee;
                //房屋上下靠结算差额
                $data['house_difference_fee'] = $house_difference_fee;
                // 奖励临时过渡费
                $data['other_subsidy_fee']=$other_subsidy_fee = $set['transition_start_time'] < $sign_time && $set['transition_end_time'] > $sign_time ? $set['reward_transition_price'] * $set['transition_month'] * ($grid_area['build_area'] + $grid_area['improve_area']): 0;
                //总计补助费
                $data['supply_total_fee'] = $supply_total_fee = $subsidy_fee + $transition_fee
                    + $require_sign_fee + $require_clear_fee + $if_decorate_fee + $stop_fee+$other_subsidy_fee;

                $party_fee = $supply_total_fee + $house_difference_fee; //相互结算总账款
                if ($party_fee > 0) {
                    $party_a_fee = abs($party_fee);
                } else {
                    $party_b_fee = abs($party_fee);
                }
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $party_a_fee ?? 0;
                //被征收人应支付征收部门费用
                $data['party_b_fee'] = $party_b_fee ?? 0;
                break;
            case '2,3': //房屋-不足35㎡
                // 搬迁补助费
                $data['subsidy_fee'] = $subsidy_fee;
                // 临时过渡费
                $transition_fee = $set['transition_price'] * $set['transition_month'] * 45;
                $data['transition_fee'] = $transition_fee;
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $stop_fee;
                //简装/不简装补助费
                $data['if_decorate_fee'] = $if_decorate_fee;
                //房屋上下靠结算差额
                $data['house_difference_fee'] = $house_difference_fee;
                //不足45支付款
                $lack_area_fee = $grid_area['lack_area'] * $set['money_price'] * 0.5;
                $data['$lack_area_fee'] = $lack_area_fee;
                // 奖励临时过渡费
                $data['other_subsidy_fee']=$other_subsidy_fee = $set['transition_start_time'] < $sign_time && $set['transition_end_time'] > $sign_time ? $set['reward_transition_price'] * $set['transition_month'] * 45: 0;
                //总计补助费
                $data['supply_total_fee'] = $supply_total_fee = $subsidy_fee + $transition_fee
                    + $require_sign_fee + $require_clear_fee + $if_decorate_fee + $stop_fee + $lack_area_fee+$other_subsidy_fee;

                $party_fee = $supply_total_fee + $house_difference_fee; //相互结算总账款
                if ($party_fee > 0) {
                    $party_a_fee = abs($party_fee);
                } else {
                    $party_b_fee = abs($party_fee);
                }
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $party_a_fee ?? 0;
                //被征收人应支付征收部门费用
                $data['party_b_fee'] = $party_b_fee ?? 0;
                break;
            case '2,4': //房屋-平房
                // 搬迁补助费
                $data['subsidy_fee'] = $subsidy_fee;
                // 临时过渡费
                $transition_fee = $set['transition_price'] * $set['transition_month'] * ($grid_area['use_area'] + $grid_area['two_area']);
                $data['transition_fee'] = $transition_fee;
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $stop_fee;
                //简装/不简装补助费
                $data['if_decorate_fee'] = $if_decorate_fee;
                //房屋上下靠结算差额
                $data['house_difference_fee'] = $house_difference_fee;
                // 奖励临时过渡费
                $data['other_subsidy_fee']=$other_subsidy_fee = $set['transition_start_time'] < $sign_time && $set['transition_end_time'] > $sign_time ? $set['reward_transition_price'] * $set['transition_month'] * ($grid_area['use_area'] + $grid_area['two_area']): 0;
                //总计补助费
                $data['supply_total_fee'] = $supply_total_fee = $subsidy_fee + $transition_fee
                    + $require_sign_fee + $require_clear_fee + $if_decorate_fee + $stop_fee+$other_subsidy_fee;

                $party_fee = $supply_total_fee + $house_difference_fee; //相互结算总账款
                if ($party_fee > 0) {
                    $party_a_fee = abs($party_fee);
                } else {
                    $party_b_fee = abs($party_fee);
                }
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $party_a_fee ?? 0;
                //被征收人应支付征收部门费用
                $data['party_b_fee'] = $party_b_fee ?? 0;
                break;
            case '2,5': //房屋-二层及以上带院楼房
                // 搬迁补助费
                $data['subsidy_fee'] = $subsidy_fee;
                // 临时过渡费
                $transition_fee = $set['transition_price'] * $set['transition_month'] * ($grid_area['use_area'] + $grid_area['two_area']);
                $data['transition_fee'] = $transition_fee;
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $stop_fee;
                //简装/不简装补助费
                $data['if_decorate_fee'] = $if_decorate_fee;
                //房屋上下靠结算差额
                $data['house_difference_fee'] = $house_difference_fee;
                // 奖励临时过渡费
                $data['other_subsidy_fee']=$other_subsidy_fee = $set['transition_start_time'] < $sign_time && $set['transition_end_time'] > $sign_time ? $set['reward_transition_price'] * $set['transition_month'] * ($grid_area['use_area'] + $grid_area['two_area']): 0;
                //总计补助费
                $data['supply_total_fee'] = $supply_total_fee = $subsidy_fee + $transition_fee
                    + $require_sign_fee + $require_clear_fee + $if_decorate_fee + $stop_fee+$other_subsidy_fee;

                $party_fee = $supply_total_fee + $house_difference_fee; //相互结算总账款
                if ($party_fee > 0) {
                    $party_a_fee = abs($party_fee);
                } else {
                    $party_b_fee = abs($party_fee);
                }
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $party_a_fee ?? 0;
                //被征收人应支付征收部门费用
                $data['party_b_fee'] = $party_b_fee ?? 0;
                break;
            case '1,1': //货币-带院住宅
                if (!$grid_assess&&!empty($grid_area['yard_area'])) {
                    return showError('请先进行评估');
                }
                // 搬迁补助费
                $data['subsidy_fee'] = $subsidy_fee;
                // 临时过渡费
                $transition_fee = $set['transition_price'] * $set['transition_month'] * ($grid_area['build_area'] + $grid_area['improve_area']);
                $data['transition_fee'] = $transition_fee;
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //货币补偿金
                $data['money_fee'] = $money_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $stop_fee;
                //院落用地及附属物、构筑物补助费
                $data['yard_fee'] = $yard_fee = ($grid_area['yard_area'] * $set['othe_build_price']) + $grid_area['yard_area'] * $grid_assess['unit_price']??0;
                // 奖励临时过渡费
                $data['other_subsidy_fee']=$other_subsidy_fee = $set['transition_start_time'] < $sign_time && $set['transition_end_time'] > $sign_time ? $set['reward_transition_price'] * $set['transition_month'] * ($grid_area['build_area'] + $grid_area['improve_area']): 0;
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $data['supply_total_fee'] = $supply_total_fee = $subsidy_fee + $transition_fee
                    + $require_sign_fee + $require_clear_fee + $stop_fee + $money_fee + $yard_fee+$other_subsidy_fee;
                break;
            case '1,2': //货币-多层楼房
                // 搬迁补助费
                $data['subsidy_fee'] = $subsidy_fee;
                // 临时过渡费
                $transition_fee = $set['transition_price'] * $set['transition_month'] * ($grid_area['build_area'] + $grid_area['improve_area']);
                $data['transition_fee'] = $transition_fee;
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //货币补偿金
                $data['money_fee'] = $money_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $stop_fee;
                // 奖励临时过渡费
                $data['other_subsidy_fee']=$other_subsidy_fee = $set['transition_start_time'] < $sign_time && $set['transition_end_time'] > $sign_time ? $set['reward_transition_price'] * $set['transition_month'] * ($grid_area['build_area'] + $grid_area['improve_area']): 0;
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $data['supply_total_fee'] = $supply_total_fee = $subsidy_fee + $transition_fee
                    + $require_sign_fee + $require_clear_fee + $stop_fee + $money_fee+$other_subsidy_fee;
                break;
            case '1,3': //货币-不足35㎡
                // 搬迁补助费
                $data['subsidy_fee'] = $subsidy_fee;
                // 临时过渡费
                $transition_fee = $set['transition_price'] * $set['transition_month'] * ($grid_area['build_area'] + $grid_area['improve_area']);
                $data['transition_fee'] = $transition_fee;
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //货币补偿金
                $data['money_fee'] = $money_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $stop_fee;
                //不足45支付款
                $lack_area_fee = $grid_area['lack_area'] * $set['money_price'] * 0.5;
                $data['$lack_area_fee'] = $lack_area_fee;
                // 奖励临时过渡费
                $data['other_subsidy_fee']=$other_subsidy_fee = $set['transition_start_time'] < $sign_time && $set['transition_end_time'] > $sign_time ? $set['reward_transition_price'] * $set['transition_month'] * ($grid_area['build_area'] + $grid_area['improve_area']): 0;
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $data['supply_total_fee'] = $supply_total_fee = $subsidy_fee + $transition_fee
                    + $require_sign_fee + $require_clear_fee + $stop_fee + $money_fee + $lack_area_fee+$other_subsidy_fee;
                break;
            case '1,4': //货币-平房
                if (!$grid_assess&&!empty($grid_area['yard_area'])) {
                    return showError('请先进行评估');
                }
                // 搬迁补助费
                $data['subsidy_fee'] = $subsidy_fee;
                // 临时过渡费
                $transition_fee = $set['transition_price'] * $set['transition_month'] * ($grid_area['build_area'] + $grid_area['improve_area']);
                $data['transition_fee'] = $transition_fee;
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //货币补偿金
                $data['money_fee'] = $money_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $stop_fee;
                //院落用地土地使用权价值
                $data['yard_fee'] = $yard_fee = $grid_area['yard_area'] * $set['othe_build_price'];
                //院落用地土地使用权价值
                $data['othe_build_fee'] = $othe_build_fee = $grid_area['yard_area'] * $grid_assess['unit_price']??0;
                // 奖励临时过渡费
                $data['other_subsidy_fee']=$other_subsidy_fee = $set['transition_start_time'] < $sign_time && $set['transition_end_time'] > $sign_time ? $set['reward_transition_price'] * $set['transition_month'] * ($grid_area['build_area'] + $grid_area['improve_area']): 0;
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $data['supply_total_fee'] = $supply_total_fee = $subsidy_fee + $transition_fee
                    + $require_sign_fee + $require_clear_fee + $stop_fee + $money_fee + $yard_fee + $othe_build_fee+$other_subsidy_fee;
                break;
            case '1,5': //货币-二层及以上带院楼房
                if (!$grid_assess&&!empty($grid_area['yard_area'])) {
                    return showError('请先进行评估');
                }
                // 搬迁补助费
                $data['subsidy_fee'] = $subsidy_fee;
                // 临时过渡费
                $transition_fee = $set['transition_price'] * $set['transition_month'] * ($grid_area['build_area'] + $grid_area['improve_area']);
                $data['transition_fee'] = $transition_fee;
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //货币补偿金
                $data['money_fee'] = $money_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $stop_fee;
                //院落用地及附属物、构筑物补助费
                $data['yard_fee'] = $yard_fee = $grid_area['yard_area'] * $set['othe_build_price'];
                //院落用地土地使用权价值
                $data['othe_build_fee'] = $othe_build_fee = $grid_area['yard_area'] * $grid_assess['unit_price']??0;
                // 奖励临时过渡费
                $data['other_subsidy_fee']=$other_subsidy_fee = $set['transition_start_time'] < $sign_time && $set['transition_end_time'] > $sign_time ? $set['reward_transition_price'] * $set['transition_month'] * ($grid_area['build_area'] + $grid_area['improve_area']): 0;
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $data['supply_total_fee'] = $supply_total_fee = $subsidy_fee + $transition_fee
                    + $require_sign_fee + $require_clear_fee + $stop_fee + $money_fee + $yard_fee + $othe_build_fee+$other_subsidy_fee;
                break;
            case '1,6': //货币-工业-集体
                if (!$grid_assess) {
                    return showError('请先进行评估');
                }
                // 搬迁补助费
                $data['subsidy_fee'] =  $grid_assess['subsidy_fee'];
                //评估补偿费
                $data['assess_fee'] = $grid_assess['total_price'];
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $grid_assess['stop_fee'];
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $data['supply_total_fee'] = $supply_total_fee = $data['subsidy_fee'] + $data['assess_fee'] + $data['require_sign_fee']
                    + $data['require_clear_fee'] + $data['stop_fee'];
                break;
            case '1,7': //货币-工业-国有
                if (!$grid_assess) {
                    return showError('请先进行评估');
                }
                // 搬迁补助费
                $data['subsidy_fee'] =  $grid_assess['subsidy_fee'];
                // 附着物补偿费
                $data['subsidiary_fee'] =  $grid_assess['attach_fee'];
                //土地使用权价值
                $data['use_fee'] = $grid_assess['unit_price'] * $grid_area['country_area'];
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $grid_assess['stop_fee'];
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $data['supply_total_fee'] = $supply_total_fee = $data['subsidy_fee'] + $data['subsidiary_fee'] + $data['use_fee'] + $data['require_sign_fee']
                    + $data['require_clear_fee'] + $data['stop_fee'];
                break;
            case '1,8': //货币-工业-国有及集体
                if (!$grid_assess) {
                    return showError('请先进行评估');
                }
                // 搬迁补助费
                $data['subsidy_fee'] =  $grid_assess['subsidy_fee'];
                // 附着物补偿费
                $data['subsidiary_fee'] =  $grid_assess['attach_fee'];
                //土地使用权价值
                $data['use_fee'] = $grid_assess['unit_price'] * $grid_area['country_area'];
                // 规定时间签约奖励费
                $data['require_sign_fee'] = $require_sign_fee;
                // 规定时间腾房奖励费
                $data['require_clear_fee'] = $require_clear_fee;
                //停产停业损失补偿金
                $data['stop_fee'] = $grid_assess['stop_fee'];
                //征收部门应支付被征收人费用
                $data['party_a_fee'] = $data['supply_total_fee'] = $supply_total_fee = $data['subsidy_fee'] + $data['subsidiary_fee'] + $data['use_fee'] + $data['require_sign_fee']
                    + $data['require_clear_fee'] + $data['stop_fee'];
                break;
            default:
                return showError('类型错误');
        }

        $data['street_id']=$grid['street_id'];
        $data['village_id']=$grid['village_id'];
        $data['project_id']=$grid['project_id'];
        $data['segment_id']=$grid['segment_id'];

        
        $res = GridPrice::query()
            ->updateOrCreate(['grid_id' => $grid['id'], 'type' => $price_type], $data);

        $res['total_area'] = $total_area ?? 0; //选择的总面积
        $res['difference_area'] = $difference_area ?? 0; //上下靠面积
        $res['require_clear_time'] = $set['require_clear_time'] ?? 0; //腾房期限
        return $res;
    }

    // 获取打印内容
    // grid 基础数据
    // set_tally 录入打印数据
    // data 打印模板
    // params 请求参数
    // find_approval 审批相关
    public function getContent($grid, $set_tally, $data, $params, $find_approval)
    {
        try {
            $content = $data['content'];

            // -----------------grid_img--------------------
            //有房产证号显示房产证号没有显示土地证号
            if (strstr($content, '{{grid_img.num_2_3}}')) {
                $li = $grid['grid_img_arr'][3][0]['num'] ?? $grid['grid_img_arr'][2][0]['num'] ?? '';
                $content = str_replace("{{grid_img.num_2_3}}", $li, $content);
            }
            //土地证
            if (strstr($content, '{{grid_img.num2}}')) {
                $li = $grid['grid_img_arr'][2][0]['num'] ?? '';
                $content = str_replace("{{grid_img.num2}}", $li, $content);
            }
            //房产证号
            if (strstr($content, '{{grid_img.num3}}')) {
                $li = $grid['grid_img_arr'][3][0]['num'] ?? '';
                $content = str_replace("{{grid_img.num3}}", $li, $content);
            }
            // -----------------grid_area--------------------
            // 土地使用面积
            if (strstr($content, '{{grid_area.use_area}}')) {
                $li = $grid['grid_area']['use_area'];
                $content = str_replace("{{grid_area.use_area}}", $li, $content);
            }
            // 房屋建筑面积
            if (strstr($content, '{{grid_area.build_area}}')) {
                $li = $grid['grid_area']['build_area'];
                $content = str_replace("{{grid_area.build_area}}", $li, $content);
            }
            // 二层及以上面积
            if (strstr($content, '{{grid_area.two_area}}')) {
                $li = $grid['grid_area']['two_area'];
                $content = str_replace("{{grid_area.two_area}}", $li, $content);
            }
            // 附属地下室建筑面积
            if (strstr($content, '{{grid_area.basement_area}}')) {
                $li = $grid['grid_area']['basement_area'];
                $content = str_replace("{{grid_area.basement_area}}", $li, $content);
            }
            // 国有面积
            if (strstr($content, '{{grid_area.country_area}}')) {
                $li = $grid['grid_area']['country_area'];
                $content = str_replace("{{grid_area.country_area}}", $li, $content);
            }
            // 集体面积
            if (strstr($content, '{{grid_area.collective_area}}')) {
                $li = $grid['grid_area']['collective_area'];
                $content = str_replace("{{grid_area.collective_area}}", $li, $content);
            }
            // 国有面积
            if (strstr($content, '{{grid_area.country_area_mu}}')) {
                $li = $grid['grid_area']['country_area']*0.0015;
                $content = str_replace("{{grid_area.country_area_mu}}", $li, $content);
            }
            // 集体面积
            if (strstr($content, '{{grid_area.collective_area_mu}}')) {
                $li = $grid['grid_area']['collective_area']*0.0015;
                $content = str_replace("{{grid_area.collective_area_mu}}", $li, $content);
            }
            // 院落面积
            if (strstr($content, '{{grid_area.yard_area}}')) {
                $li = $grid['grid_area']['yard_area'];
                $content = str_replace("{{grid_area.yard_area}}", $li, $content);
            }
            // 公摊面积
            if (strstr($content, '{{grid_area.public_area}}')) {
                $li = $grid['grid_area']['public_area'];
                $content = str_replace("{{grid_area.public_area}}", $li, $content);
            }
            // 应补偿面积
            if (strstr($content, '{{grid_area.supply_area}}')) {
                $li = $grid['grid_area']['supply_area'];
                $content = str_replace("{{grid_area.supply_area}}", $li, $content);
            }
            // 兼用面积
            if (strstr($content, '{{grid_area.dualuse_area}}')) {
                $li = $grid['grid_area']['dualuse_area'];
                $content = str_replace("{{grid_area.dualuse_area}}", $li, $content);
            }
            // 奖励面积
            if (strstr($content, '{{grid_area.reward_area}}')) {
                $li = $grid['grid_area']['reward_area'];
                $content = str_replace("{{grid_area.reward_area}}", $li, $content);
            }
            // 改善面积
            if (strstr($content, '{{grid_area.improve_area}}')) {
                $li = $grid['grid_area']['improve_area'];
                $content = str_replace("{{grid_area.improve_area}}", $li, $content);
            }
            // 不足45㎡面积
            if (strstr($content, '{{grid_area.lack_area}}')) {
                $li = $grid['grid_area']['lack_area'];
                $content = str_replace("{{grid_area.lack_area}}", $li, $content);
            }
            // 测绘绘制图照片
            if (strstr($content, '{{grid_area.mapping_content}}')) {
                $mapping_content=json_decode($grid['grid_area']['mapping_content'],true);
                $li="";
                if($mapping_content){
                    foreach($mapping_content as $v){
                        $li .= $v['img']? '<p style="display:flex;flex-direction:column;"><img width="400px" src="' . $this->getImageUrl($v['img'])['remote_url'] . '" style="align-self:center;">&nbsp' : '';
                    }
                    $content = str_replace("{{grid_area.mapping_content}}", $li, $content);
                }elseif($grid['grid_area']['area_imgs']){
                    $area_imgs=explode(',',$grid['grid_area']['area_imgs']);
                    foreach($area_imgs as $v){
                        $li .= $v? '<img width="100%" src="' . $this->getImageUrl($v)['remote_url'] . '">&nbsp' : '';
                    }
                    $content=$li;
                }else{
                    $content = str_replace("{{grid_area.mapping_content}}", $li, $content);
                }
            }
            $ruhu_area = GridArea::query()->where('grid_id', $grid['id'])->where('type', 1)->first();
            // -----------------grid_area_ruhu--------------------
            // 土地使用面积
            if (strstr($content, '{{grid_area.use_area_ruhu}}')) {
                $li = $ruhu_area['use_area'];
                $content = str_replace("{{grid_area.use_area_ruhu}}", $li, $content);
            }
            // 房屋建筑面积
            if (strstr($content, '{{grid_area.build_area_ruhu}}')) {
                $li = $ruhu_area['build_area'];
                $content = str_replace("{{grid_area.build_area_ruhu}}", $li, $content);
            }
            // 二层及以上面积
            if (strstr($content, '{{grid_area.two_area_ruhu}}')) {
                $li = $ruhu_area['two_area'];
                $content = str_replace("{{grid_area.two_area_ruhu}}", $li, $content);
            }
             // 附属地下室建筑面积
             if (strstr($content, '{{grid_area.basement_area_ruhu}}')) {
                $li = $ruhu_area['basement_area'];
                $content = str_replace("{{grid_area.basement_area_ruhu}}", $li, $content);
            }
            // 国有面积
            if (strstr($content, '{{grid_area.country_area_ruhu}}')) {
                $li = $ruhu_area['country_area'];
                $content = str_replace("{{grid_area.country_area_ruhu}}", $li, $content);
            }
            // 集体面积
            if (strstr($content, '{{grid_area.collective_area_ruhu}}')) {
                $li = $ruhu_area['collective_area'];
                $content = str_replace("{{grid_area.collective_area_ruhu}}", $li, $content);
            }
            // 国有面积换成亩
            if (strstr($content, '{{grid_area.country_area_mu_ruhu}}')) {
                $li = $ruhu_area['country_area']*0.0015;
                $content = str_replace("{{grid_area.country_area_mu_ruhu}}", $li, $content);
            }
            // 集体面积换成亩
            if (strstr($content, '{{grid_area.collective_area_mu_ruhu}}')) {
                $li = $ruhu_area['collective_area']*0.0015;
                $content = str_replace("{{grid_area.collective_area_mu_ruhu}}", $li, $content);
            }
            // 院落面积
            if (strstr($content, '{{grid_area.yard_area_ruhu}}')) {
                $li = $ruhu_area['yard_area'];
                $content = str_replace("{{grid_area.yard_area_ruhu}}", $li, $content);
            }
            // 公摊面积
            if (strstr($content, '{{grid_area.public_area_ruhu}}')) {
                $li = $ruhu_area['public_area'];
                $content = str_replace("{{grid_area.public_area_ruhu}}", $li, $content);
            }
            // 应补偿面积
            if (strstr($content, '{{grid_area.supply_area_ruhu}}')) {
                $li = $ruhu_area['supply_area'];
                $content = str_replace("{{grid_area.supply_area_ruhu}}", $li, $content);
            }
            // 兼用面积
            if (strstr($content, '{{grid_area.dualuse_area_ruhu}}')) {
                $li = $ruhu_area['dualuse_area'];
                $content = str_replace("{{grid_area.dualuse_area_ruhu}}", $li, $content);
            }
            // 奖励面积
            if (strstr($content, '{{grid_area.reward_area_ruhu}}')) {
                $li = $ruhu_area['reward_area'];
                $content = str_replace("{{grid_area.reward_area_ruhu}}", $li, $content);
            }
            // 改善面积
            if (strstr($content, '{{grid_area.improve_area_ruhu}}')) {
                $li = $ruhu_area['improve_area'];
                $content = str_replace("{{grid_area.improve_area_ruhu}}", $li, $content);
            }
            // 不足45㎡面积
            if (strstr($content, '{{grid_area.lack_area_ruhu}}')) {
                $li = $ruhu_area['lack_area'];
                $content = str_replace("{{grid_area.lack_area_ruhu}}", $li, $content);
            }
            // -----------------grid_assess--------------------
            $grid_assess=GridAssess::query()->where('grid_id', $grid['id'])->first();
            //土地使用权价值单价
            if (strstr($content, '{{grid_assess.unit_price}}')) {
                $li = $grid_assess['unit_price'];
                $content = str_replace("{{grid_assess.unit_price}}", $li, $content);
            }
            //评估补偿费总价
            if (strstr($content, '{{grid_assess.total_price}}')) {
                $li = $grid_assess['total_price'];
                $content = str_replace("{{grid_assess.total_price}}", $li, $content);
            }
            // 评估详情列表
            if (strstr($content, '{{grid.grid_assess_list}}')) {
                // $li = '<table border="1" cellspacing="0" style="text-align: center; width: 100%">
                $li='';
                if($grid_assess['content']){
                    $li = '<table border="0" class="border" cellspacing="0" frame=void style="text-align: center; width: 100%">
                    <tr>
                    <th>序号</th>
                    <th>权利人</th>
                    <th>网格号</th>
                    <th>项目名称</th>
                    <th>类别</th>
                    <th>单位</th>
                    <th>数量</th>
                    <th>单价</th>
                    <th>总价</th>
                    <th>备注</th>
                    </tr>';
                    if($grid_assess){
                        $grid_content=json_decode($grid_assess['content'],true);
                        if($grid_content){
                            foreach ($grid_content as $k1 => $v1) {
                                $k=$k1+1;
                                $li .= "<tr height='50px'>
                                    <td>$k</td>
                                    <td>" . implode(',', array_column($grid['villager'], 'name')) . "</td>
                                    <td>" . $grid['name'] . "</td>
                                    <td>" . $v1['name'] . "</td>
                                    <td>" . $v1['pid_name'] . "</td>
                                    <td>" . $v1['unit'] . "</td>
                                    <td>" . $v1['num'] . "</td>
                                    <td>" . $v1['unit_price'] . "</td>
                                    <td>" . $v1['total_price'] . "</td>
                                    <td>" . $v1['remarks'] . "</td>
                                </tr>";
                            }
                        }
                    }
                    $li .= '</table>';
                    $content = str_replace("{{grid.grid_assess_list}}", $li, $content);
                }elseif($grid_assess['assess_imgs']){
                    $assess_imgs=explode(',',$grid_assess['assess_imgs']);
                    foreach($assess_imgs as $v){
                        $li .= $v? '<img width="100%" src="' . $this->getImageUrl($v)['remote_url'] . '">&nbsp' : '';
                    }
                    $content=$li;
                }else{
                    $content = str_replace("{{grid_area.grid_assess_list}}", $li, $content);
                }
               
            }
            // -----------------grid--------------------
            // 营业执照编号

            if (strstr($content, '{{grid.business_num}}')) {
                $li = $grid['business_num'];
                $content = str_replace("{{grid.business_num}}", $li, $content);
            }
            // 营业执照颁发时间
            if (strstr($content, '{{grid.business_award_time}}')) {
                $li = $grid['business_award_time'];
                $content = str_replace("{{grid.business_award_time}}", $li, $content);
            }
            // 路户号
            if (strstr($content, '{{grid.address}}')) {
                $li = $grid['address'];
                $content = str_replace("{{grid.address}}", $li, $content);
            }
            // 网格编号
            if (strstr($content, '{{grid.name}}')) {
                $li = $grid['name'];
                $content = str_replace("{{grid.name}}", $li, $content);
            }
            //房屋形态
            if (strstr($content, '{{grid.build_form}}')) {
                $li = BuildForm::query()->where('id', $grid['build_form'])->value('name');
                $content = str_replace("{{grid.build_form}}", $li, $content);
            }
            //房屋形态
            if (strstr($content, '{{grid.house_use}}')) {
                $li = $grid['house_use'] == 1 ? '住宅' : '非住宅';
                $content = str_replace("{{grid.house_use}}", $li, $content);
            }
            // 街道
            if (strstr($content, '{{grid.street}}')) {
                $li = Street::query()->where('id', $grid['street_id'])->value('name');
                $content = str_replace("{{grid.street}}", $li, $content);
            }
            // 村庄
            if (strstr($content, '{{grid.village}}')) {
                $li = Village::query()->where('id', $grid['village_id'])->value('name');
                $content = str_replace("{{grid.village}}", $li, $content);
            }
            // 村民姓名
            if (strstr($content, '{{grid.villager_name}}')) {
                $li = implode('、', array_column($grid['villager'], 'name'));
                $content = str_replace("{{grid.villager_name}}", $li, $content);
            }
            // 村民身份证
            if (strstr($content, '{{grid.identity_card}}')) {
                $li = implode('<br>', array_column($grid['villager'], 'identity_card'));
                $content = str_replace("{{grid.identity_card}}", $li, $content);
            }
            // 村民代理人列表
            if (strstr($content, '{{grid.villager}}')) {
                // $li = '<table border="1" cellspacing="0" style="text-align: center; width: 100%">
                $li = '<table border="0" class="border" cellspacing="0" frame=void style="text-align: center; width: 100%">
                <tr>
                <th>被搬迁人</th>
                <th>姓名</th>
                <th>身份证号码</th>
                <th>联系电话</th>
                </tr>';
               if ($grid['villager']) {
                   foreach ($grid['villager'] as $v1) {
                       $li .= "<tr height='50px'>
                        <td>权利人</td>
                        <td>" . $v1['name'] . "</td>
                        <td>" . $v1['identity_card'] . "</td>
                        <td>" . $v1['phone'] . "</td>
                    </tr>";
                   }
               }

               if ($grid['agent']) {
                   $li .= "<tr height='50px'>
                        <td>代理人</td>
                        <td>" . $grid['agent']['name'] . "</td>
                        <td>" . $grid['agent']['identity_card'] . "</td>
                        <td>" . $grid['agent']['phone'] . "</td>
                    </tr>";
               }
               $li .= '</table>';
               $content = str_replace("{{grid.villager}}", $li, $content);
           }
            // -----------------grid_price--------------------
            // 搬迁补助费
            if (strstr($content, '{{grid_price.subsidy_fee}}')) {
                $li = $grid['grid_price']['subsidy_fee'];
                $content = str_replace("{{grid_price.subsidy_fee}}", $li, $content);
            }
            // 临时过渡费
            if (strstr($content, '{{grid_price.transition_fee}}')) {
                $li = $grid['grid_price']['transition_fee'];
                $content = str_replace("{{grid_price.transition_fee}}", $li, $content);
            }
            // 奖励临时过渡费
            if (strstr($content, '{{grid_price.other_subsidy_fee}}')) {
                $li = $grid['grid_price']['other_subsidy_fee'];
                $content = str_replace("{{grid_price.other_subsidy_fee}}", $li, $content);
            }
            // 临时过渡费+奖励临时过渡费
            if (strstr($content, '{{grid_price.transition_subsidy_fee}}')) {
                $li = $grid['grid_price']['transition_fee']+$grid['grid_price']['other_subsidy_fee'];
                $content = str_replace("{{grid_price.transition_subsidy_fee}}", $li, $content);
            }
            // 规定时间签约奖励费
            if (strstr($content, '{{grid_price.require_sign_fee}}')) {
                $li = $grid['grid_price']['require_sign_fee'];
                $content = str_replace("{{grid_price.require_sign_fee}}", $li, $content);
            }
            // 规定时间腾房奖励费
            if (strstr($content, '{{grid_price.require_clear_fee}}')) {
                $li = $grid['grid_price']['require_clear_fee'];
                $content = str_replace("{{grid_price.require_clear_fee}}", $li, $content);
            }
            // 简装/不简装补助费
            if (strstr($content, '{{grid_price.if_decorate_fee}}')) {
                $li = $grid['grid_price']['if_decorate_fee'];
                $content = str_replace("{{grid_price.if_decorate_fee}}", $li, $content);
            }
            // 停产停业损失补偿金
            if (strstr($content, '{{grid_price.stop_fee}}')) {
                $li = $grid['grid_price']['stop_fee'];
                $content = str_replace("{{grid_price.stop_fee}}", $li, $content);
            }
            // 货币补偿金
            if (strstr($content, '{{grid_price.money_fee}}')) {
                $li = $grid['grid_price']['money_fee'];
                $content = str_replace("{{grid_price.money_fee}}", $li, $content);
            }
            // 不足45㎡结算差价
            if (strstr($content, '{{grid_price.lack_area_fee}}')) {
                $li = $grid['grid_price']['lack_area_fee'];
                $content = str_replace("{{grid_price.lack_area_fee}}", $li, $content);
            }
            // 院落用地及附属物、构筑物补助费
            if (strstr($content, '{{grid_price.yard_fee}}')) {
                $li = $grid['grid_price']['yard_fee'];
                $content = str_replace("{{grid_price.yard_fee}}", $li, $content);
            }
            // 院落用地土地使用权价值
            if (strstr($content, '{{grid_price.othe_build_fee}}')) {
                $li = $grid['grid_price']['othe_build_fee'];
                $content = str_replace("{{grid_price.othe_build_fee}}", $li, $content);
            }
            // 院落用地土地使用权价值+院落用地及附属物、构筑物补助费
            if (strstr($content, '{{grid_price.othe_build_fee_yard_fee}}')) {
                $li = $grid['grid_price']['othe_build_fee']+$grid['grid_price']['yard_fee'];
                $content = str_replace("{{grid_price.othe_build_fee_yard_fee}}", $li, $content);
            }
            // 土地使用权价值
            if (strstr($content, '{{grid_price.use_fee}}')) {
                $li = $grid['grid_price']['use_fee'];
                $content = str_replace("{{grid_price.use_fee}}", $li, $content);
            }
            // 评估补偿费
            if (strstr($content, '{{grid_price.assess_fee}}')) {
                $li = $grid['grid_price']['assess_fee'];
                $content = str_replace("{{grid_price.assess_fee}}", $li, $content);
            }
            // 附着物补偿费
            if (strstr($content, '{{grid_price.subsidiary_fee}}')) {
                $li = $grid['grid_price']['subsidiary_fee'];
                $content = str_replace("{{grid_price.subsidiary_fee}}", $li, $content);
            }
            // 总计补助费
            if (strstr($content, '{{grid_price.supply_total_fee}}')) {
                $li = $grid['grid_price']['supply_total_fee'];
                $content = str_replace("{{grid_price.supply_total_fee}}", $li, $content);
            }
            // 总计补助费(不加停产停业)
            if (strstr($content, '{{grid_price.supply_total_fee_2}}')) {
                $li = $grid['grid_price']['supply_total_fee']-$grid['grid_price']['stop_fee'];
                $content = str_replace("{{grid_price.supply_total_fee_2}}", $li, $content);
            }
            // 总计补助费大写
            if (strstr($content, '{{grid_price.supply_total_fee_big}}')) {
                $li = $this->num_to_rmb($grid['grid_price']['supply_total_fee']);
                $content = str_replace("{{grid_price.supply_total_fee_big}}", $li, $content);
            }
            // 房屋上下靠结算差额的绝对值
            if (strstr($content, '{{grid_price.house_difference_fee}}')) {
                $li = abs($grid['grid_price']['house_difference_fee']);
                $content = str_replace("{{grid_price.house_difference_fee}}", $li, $content);
            }
            // 上靠/下靠
            if (strstr($content, '{{grid_price.upper_lower}}')) {
                $li = $grid['grid_price']['house_difference_fee'] > 0 ? '下靠' : '上靠';
                $content = str_replace("{{grid_price.upper_lower}}", $li, $content);
            }
            // (上靠/下靠)面积
            if (strstr($content, '{{grid_price.difference_area}}')) {
                $li = abs($set_tally['difference_area']);
                $content = str_replace("{{grid_price.difference_area}}", $li, $content);
            }
            // 征收部门应支付被征收人费用
            if (strstr($content, '{{grid_price.party_a_fee}}')) {
                $li = $grid['grid_price']['party_a_fee'];
                $content = str_replace("{{grid_price.party_a_fee}}", $li, $content);
            }
            // 征收部门应支付被征收人费用大写
            if (strstr($content, '{{grid_price.party_a_fee_big}}')) {
                $li = $this->num_to_rmb($grid['grid_price']['party_a_fee']);
                $content = str_replace("{{grid_price.party_a_fee_big}}", $li, $content);
            }
            // 被征收人应支付征收部门费用
            if (strstr($content, '{{grid_price.party_b_fee}}')) {
                $li = $grid['grid_price']['party_b_fee'];
                $content = str_replace("{{grid_price.party_b_fee}}", $li, $content);
            }
            // 被征收人应支付征收部门费用大写
            if (strstr($content, '{{grid_price.party_b_fee_big}}')) {
                $li = $this->num_to_rmb($grid['grid_price']['party_b_fee']);
                $content = str_replace("{{grid_price.party_b_fee_big}}", $li, $content);
            }
            // 谁应向谁支付差额面积房款(甲方应向乙方/乙方应向甲方)
            if (strstr($content, '{{grid_price.a_or_b}}')) {
                $li = $set_tally['difference_area'] > 0 ? '甲方应向乙方' : '乙方应向甲方';
                $content = str_replace("{{grid_price.a_or_b}}", $li, $content);
            }
            // -----------------grid_house--------------------
            // 选择的总面积
            if (strstr($content, '{{grid_house.grid_house_area}}')) {
                $li = $set_tally['total_area'];
                $content = str_replace("{{grid_house.grid_house_area}}", $li, $content);
            }
            // 选择的总套数
            if (strstr($content, '{{grid_house.grid_house_num}}')) {
                $li = count($grid['grid_house']);
                $content = str_replace("{{grid_house.grid_house_num}}", $li, $content);
            }
            // 不简单装修( X  户型 X 套、)
            if (strstr($content, '{{grid_house.no_decorate}}')) {
                $li='';
                foreach ($grid['grid_house'] as  $k => $v2) {
                    if($v2['is_decorate']==2){
                        $li.=$v2['house_type']['name'].'户型 1 套、';
                    }
                }
                $content = str_replace("{{grid_house.no_decorate}}", $li, $content);
            }
            // 户型列表
            if (strstr($content, '{{grid_house.grid_house_list}}')) {
                $li = '<table  border="1" cellspacing="0" style="text-align: center; width: 100%">
                 <tr>
                     <td width="125" rowspan="2">建筑面积(㎡)</td>
                     <td width="217" colspan="2">选房情况</td>
                     <td width="167" rowspan="2">房屋权利人</td>
                 </tr>
                 <tr>
                     <td width="73">套数</td>
                     <td width="143">面积</td>
                 </tr>';

                foreach ($grid['grid_house'] as  $k => $v2) {

                    $villager = Villager::query()->whereIn('id', explode(',', $v2['villager_ids']))->pluck('name')->toArray();

                    $li .= '<tr>
                         <td width="125">' . $v2['house_type']['area'] . '</td>
                         <td width="73">1</td>
                         <td width="143">' . $v2['house_type']['area'] . '</td>
                         <td width="167">' . implode(',', $villager) . '</td>
                     </tr>';
                }
                $li .= '</table>';
                $content = str_replace("{{grid_house.grid_house_list}}", $li, $content);
            }
            // -----------------其他--------------------
            // 协议自增编号(按 项目 补偿方式 模板类型 自增)
            if (strstr($content, '{{other.auto_num_1}}')) {
                if($params['type']==2){
                    $auto_num = PrintLog::query()
                        ->where('grid_id', $grid['id'])
                        ->where('print_type', 9)
                        ->where('sign_set_type', $params['sign_set_type'])
                        ->where('supply_form', $grid['supply_form'])
                        ->value('auto_num');

                    if (!$auto_num) {
                        $auto_num = PrintLog::query()
                            ->where('project_id', $grid['project_id'])
                            ->where('sign_set_type', $params['sign_set_type'])
                            ->where('print_type', 9)
                            ->where('supply_form', $grid['supply_form'])
                            ->max('auto_num') + 1;
                    }

                    $li = $auto_num;
                }else{
                    $li = '';
                }
                $content = str_replace("{{other.auto_num_1}}", $li, $content);
            }

            // 协议自增编号(按 项目 模板类型 自增)
            if (strstr($content, '{{other.auto_num_2}}')) {
                if($params['type']==2){
                    $auto_num = PrintLog::query()
                        ->where('grid_id', $grid['id'])
                        ->where('print_type', 9)
                        ->where('sign_set_type', $params['sign_set_type'])
                        ->value('auto_num');

                    if (!$auto_num) {
                        $auto_num = PrintLog::query()
                            ->where('project_id', $grid['project_id'])
                            ->where('sign_set_type', $params['sign_set_type'])
                            ->where('print_type', 9)
                            ->max('auto_num') + 1;
                    }

                    $li = $auto_num;
                }else{
                    $li = '';
                }
                $content = str_replace("{{other.auto_num_2}}", $li, $content);
            }

            // 协议自增编号(按 项目 建筑形式 模板类型 自增)
            if (strstr($content, '{{other.auto_num_3}}')) {
                if($params['type']==2){
                    $auto_num = PrintLog::query()
                    ->where('grid_id', $grid['id'])
                    ->where('print_type', 9)
                    ->where('sign_set_type', $params['sign_set_type'])
                    ->where('build_form', $grid['build_form'])
                    ->value('auto_num');

                    if (!$auto_num) {
                        $auto_num = PrintLog::query()
                            ->where('project_id', $grid['project_id'])
                            ->where('sign_set_type', $params['sign_set_type'])
                            ->where('print_type', 9)
                            ->where('build_form', $grid['build_form'])
                            ->max('auto_num') + 1;
                    }

                    $li = $auto_num;
                }else{
                    $li = '';
                }
                $content = str_replace("{{other.auto_num_3}}", $li, $content);
            }

            // 签约协议编号
            if (strstr($content, '{{other.sign_num}}')) {
                $li = $grid['sign_num'];
                $content = str_replace("{{other.sign_num}}", $li, $content);
            }

            //当前时间
            if (strstr($content, '{{other.date}}')) {
                $li = date('Y年m月d日');
                $content = str_replace("{{other.date}}", $li, $content);
            }

            //签约时间
            if (strstr($content, '{{other.sign_time}}')) {
                $li = $grid['sign_time']?date('Y年m月d日',strtotime($grid['sign_time'])):date('Y年m月d日');
                $content = str_replace("{{other.sign_time}}", $li, $content);
            }

            //腾房时间
            if (strstr($content, '{{other.require_clear_time}}')) {
                $require_clear_time = ProtocolSet::query()
                ->where('supply_form', $grid['supply_form'])
                ->where('build_form', $grid['build_form'])
                ->where('street_id', $grid['street_id'])
                ->where('village_id', $grid['village_id'])
                ->where('project_id', $grid['project_id'])
                ->where('segment_id', $grid['segment_id'])
                ->value('require_clear_time');
                if (empty($require_clear_time)) {
                    return showError('未查询到配置信息，请联系管理员到后台配置');
                }
                $li=date('Y年m月d日',$grid['sign_time']?strtotime($grid['sign_time']):time()+$require_clear_time*24*60*60);
                $content = str_replace("{{other.require_clear_time}}", $li, $content);
            }

            // 权益分配列表
            if (strstr($content, '{{other.protocol_plan}}')) {
                $protocol_plan=ProtocolPlan::query()->where('grid_id',$grid['id'])->get()->toArray();

                if($protocol_plan){
                    $li = '权益分配：<br><table  border="1" cellspacing="0" style="text-align: center; width: 100%">
                    <tr>
                        <td style="font-weight: bold;">序号</td>
                        <td style="font-weight: bold;">项目</td>
                        <td style="font-weight: bold;">原房屋权益</td>';
                        foreach($protocol_plan as $v){
                            $villager=Villager::query()->select('name')->whereIn('id',explode(',',$v['villager_ids']))->get()->toArray();
                            
                            $li .= '<td style="font-weight: bold;">'.implode(',',array_column($villager,'name')).'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>1</td>
                        <td>搬迁补助费</td>
                        <td>'.$grid['grid_price']['subsidy_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['subsidy_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>2</td>
                        <td>临时过渡费</td>
                        <td>'.$grid['grid_price']['transition_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['transition_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>3</td>
                        <td>规定时间签约奖励费</td>
                        <td>'.$grid['grid_price']['require_sign_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['require_sign_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>4</td>
                        <td>规定时间腾房奖励费</td>
                        <td>'.$grid['grid_price']['require_clear_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['require_clear_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>5</td>
                        <td>简装/不简装补助费</td>
                        <td>'.$grid['grid_price']['if_decorate_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['if_decorate_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>6</td>
                        <td>奖励临时过渡费</td>
                        <td>'.$grid['grid_price']['other_subsidy_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['other_subsidy_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>7</td>
                        <td>停产停业损失补偿金</td>
                        <td>'.$grid['grid_price']['stop_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['stop_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>8</td>
                        <td>不足45㎡结算差价</td>
                        <td>'.$grid['grid_price']['lack_area_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['lack_area_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>9</td>
                        <td>货币补偿金</td>
                        <td>'.$grid['grid_price']['money_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['money_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>10</td>
                        <td>院落用地及附属物、构筑物补助费</td>
                        <td>'.$grid['grid_price']['yard_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['yard_fee'].'</td>';
                        }
                    $li .= '</tr>';
                    
                    $li .= '<tr>
                        <td>11</td>
                        <td>院落用地土地使用权价值</td>
                        <td>'.$grid['grid_price']['othe_build_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['othe_build_fee'].'</td>';
                        }
                    $li .= '</tr>';
                    
                    $li .= '<tr>
                        <td>12</td>
                        <td>评估补偿费</td>
                        <td>'.$grid['grid_price']['assess_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['assess_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>13</td>
                        <td>土地使用权价值</td>
                        <td>'.$grid['grid_price']['use_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['use_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>14</td>
                        <td>附着物补偿费</td>
                        <td>'.$grid['grid_price']['subsidiary_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['subsidiary_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>15</td>
                        <td>房屋上下靠结算差额</td>
                        <td>'.$grid['grid_price']['house_difference_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['house_difference_fee'].'</td>';
                        }
                    $li .= '</tr>';
    
                    $li .= '<tr>
                        <td>16</td>
                        <td>总计金额</td>
                        <td>'.$grid['grid_price']['party_a_fee'].'</td>';
                        foreach($protocol_plan as $v){
                            $li .= '<td>'.$v['party_a_fee'].'</td>';
                        }
                    $li .= '</tr>';
                  
                   $li .= '</table>';
                }else{
                    $li ='';
                }
              
                $content = str_replace("{{other.protocol_plan}}", $li, $content);
            }

            // 全部审批人签章
            if (strstr($content, '{{other.approval_sign}}')) {
                $approval_log = ApprovalLog::query()
                    ->where('list_id', $find_approval['id']??0)
                    ->where('apply_result', '<>', '')
                    ->get()
                    ->toArray();
                $li = '';
                foreach ($approval_log as $v) {
                    $signatures_img = User::query()->where('id', $v['apply_user_id'])->value('signatures_img');
                    $li .= $signatures_img ? '<img width="100px" src="' . $this->getImageUrl($signatures_img)['remote_url'] . '"><br>' : '';
                    $li .= $signatures_img ? date('Y年m月d日', strtotime($v['approval_time'])) . '<br>' : '';
                }
                $content = str_replace("{{other.approval_sign}}", $li, $content);
            }

            // 第一环节审批人签章和时间
            if (strstr($content, '{{other.approval_sign_1}}')) {
                $approval_log = ApprovalLog::query()
                    ->where('list_id', $find_approval['id']??0)
                    ->where('apply_result', '<>', '')
                    ->where('step', 1)
                    ->first();
                $li = '';
                if ($approval_log) {
                    $signatures_img = User::query()->where('id', $approval_log['apply_user_id'])->value('signatures_img');
                    $li .= $signatures_img ? '<img width="100px" src="' . $this->getImageUrl($signatures_img)['remote_url'] . '"><br>' : '';
                    $li .= $signatures_img ? date('Y年m月d日', strtotime($approval_log['approval_time'])) . '<br>' : '';
                }

                $content = str_replace("{{other.approval_sign_1}}", $li, $content);
            }
            // 第二环节审批人签章和时间
            if (strstr($content, '{{other.approval_sign_2}}')) {
                $approval_log = ApprovalLog::query()
                    ->where('list_id', $find_approval['id']??0)
                    ->where('apply_result', '<>', '')
                    ->where('step', 2)
                    ->first();
                $li = '';
                if ($approval_log) {
                    $signatures_img = User::query()->where('id', $approval_log['apply_user_id'])->value('signatures_img');
                    $li .= $signatures_img ? '<img width="100px" src="' . $this->getImageUrl($signatures_img)['remote_url'] . '"><br>' : '';
                    $li .= $signatures_img ? date('Y年m月d日', strtotime($approval_log['approval_time'])) . '<br>' : '';
                }
                $content = str_replace("{{other.approval_sign_2}}", $li, $content);
            }
            // 第三环节审批人签章和时间
            if (strstr($content, '{{other.approval_sign_3}}')) {
                $approval_log = ApprovalLog::query()
                    ->where('list_id', $find_approval['id']??0)
                    ->where('apply_result', '<>', '')
                    ->where('step', 3)
                    ->first();
                $li = '';
                if ($approval_log) {
                    $signatures_img = User::query()->where('id', $approval_log['apply_user_id'])->value('signatures_img');
                    $li .= $signatures_img ? '<img width="100px" src="' . $this->getImageUrl($signatures_img)['remote_url'] . '"><br>' : '';
                    $li .= $signatures_img ? date('Y年m月d日', strtotime($approval_log['approval_time'])) . '<br>' : '';
                }
                $content = str_replace("{{other.approval_sign_3}}", $li, $content);
            }
            // 第四环节审批人签章和时间
            if (strstr($content, '{{other.approval_sign_4}}')) {
                $approval_log = ApprovalLog::query()
                    ->where('list_id', $find_approval['id']??0)
                    ->where('apply_result', '<>', '')
                    ->where('step', 4)
                    ->first();
                $li = '';
                if ($approval_log) {
                    $signatures_img = User::query()->where('id', $approval_log['apply_user_id'])->value('signatures_img');
                    $li .= $signatures_img ? '<img width="100px" src="' . $this->getImageUrl($signatures_img)['remote_url'] . '"><br>' : '';
                    $li .= $signatures_img ? date('Y年m月d日', strtotime($approval_log['approval_time'])) . '<br>' : '';
                }
                $content = str_replace("{{other.approval_sign_4}}", $li, $content);
            }
            // 第五环节审批人签章和时间
            if (strstr($content, '{{other.approval_sign_5}}')) {
                $approval_log = ApprovalLog::query()
                    ->where('list_id', $find_approval['id']??0)
                    ->where('apply_result', '<>', '')
                    ->where('step', 5)
                    ->first();
                $li = '';
                if ($approval_log) {
                    $signatures_img = User::query()->where('id', $approval_log['apply_user_id'])->value('signatures_img');
                    $li .= $signatures_img ? '<img width="100px" src="' . $this->getImageUrl($signatures_img)['remote_url'] . '"><br>' : '';
                    $li .= $signatures_img ? date('Y年m月d日', strtotime($approval_log['approval_time'])) . '<br>' : '';
                }
                $content = str_replace("{{other.approval_sign_5}}", $li, $content);
            }
            // 第六环节审批人签章和时间
            if (strstr($content, '{{other.approval_sign_6}}')) {
                $approval_log = ApprovalLog::query()
                    ->where('list_id', $find_approval['id']??0)
                    ->where('apply_result', '<>', '')
                    ->where('step', 6)
                    ->first();
                $li = '';
                if ($approval_log) {
                    $signatures_img = User::query()->where('id', $approval_log['apply_user_id'])->value('signatures_img');
                    $li .= $signatures_img ? '<img width="100px" src="' . $this->getImageUrl($signatures_img)['remote_url'] . '"><br>' : '';
                    $li .= $signatures_img ? date('Y年m月d日', strtotime($approval_log['approval_time'])) . '<br>' : '';
                }
                $content = str_replace("{{other.approval_sign_6}}", $li, $content);
            }
            // 第七环节审批人签章和时间
            if (strstr($content, '{{other.approval_sign_7}}')) {
                $approval_log = ApprovalLog::query()
                    ->where('list_id', $find_approval['id']??0)
                    ->where('apply_result', '<>', '')
                    ->where('step', 7)
                    ->first();
                $li = '';
                if ($approval_log) {
                    $signatures_img = User::query()->where('id', $approval_log['apply_user_id'])->value('signatures_img');
                    $li .= $signatures_img ? '<img width="100px" src="' . $this->getImageUrl($signatures_img)['remote_url'] . '"><br>' : '';
                    $li .= $signatures_img ? date('Y年m月d日', strtotime($approval_log['approval_time'])) . '<br>' : '';
                }
                $content = str_replace("{{other.approval_sign_7}}", $li, $content);
            }

            // 第一环节审批人签章
            if (strstr($content, '{{other.approval_sign_sign_1}}')) {
                $approval_log = ApprovalLog::query()
                    ->where('list_id', $find_approval['id']??0)
                    ->where('apply_result', '<>', '')
                    ->where('step', 1)
                    ->first();
                $li = '';
                if ($approval_log) {
                    $signatures_img = User::query()->where('id', $approval_log['apply_user_id'])->value('signatures_img');
                    $li .= $signatures_img ? '<img width="100px" src="' . $this->getImageUrl($signatures_img)['remote_url'] . '"><br>' : '';
                }

                $content = str_replace("{{other.approval_sign_sign_1}}", $li, $content);
            }
            // 第二环节审批人签章
            if (strstr($content, '{{other.approval_sign_sign_2}}')) {
                $approval_log = ApprovalLog::query()
                    ->where('list_id', $find_approval['id']??0)
                    ->where('apply_result', '<>', '')
                    ->where('step', 2)
                    ->first();
                $li = '';
                if ($approval_log) {
                    $signatures_img = User::query()->where('id', $approval_log['apply_user_id'])->value('signatures_img');
                    $li .= $signatures_img ? '<img width="100px" src="' . $this->getImageUrl($signatures_img)['remote_url'] . '"><br>' : '';
                }
                $content = str_replace("{{other.approval_sign_sign_2}}", $li, $content);
            }
            // 第三环节审批人签章
            if (strstr($content, '{{other.approval_sign_sign_3}}')) {
                $approval_log = ApprovalLog::query()
                    ->where('list_id', $find_approval['id']??0)
                    ->where('apply_result', '<>', '')
                    ->where('step', 3)
                    ->first();
                $li = '';
                if ($approval_log) {
                    $signatures_img = User::query()->where('id', $approval_log['apply_user_id'])->value('signatures_img');
                    $li .= $signatures_img ? '<img width="100px" src="' . $this->getImageUrl($signatures_img)['remote_url'] . '"><br>' : '';
                }
                $content = str_replace("{{other.approval_sign_sign_3}}", $li, $content);
            }
            // 第四环节审批人签章
            if (strstr($content, '{{other.approval_sign_sign_4}}')) {
                $approval_log = ApprovalLog::query()
                    ->where('list_id', $find_approval['id']??0)
                    ->where('apply_result', '<>', '')
                    ->where('step', 4)
                    ->first();
                $li = '';
                if ($approval_log) {
                    $signatures_img = User::query()->where('id', $approval_log['apply_user_id'])->value('signatures_img');
                    $li .= $signatures_img ? '<img width="100px" src="' . $this->getImageUrl($signatures_img)['remote_url'] . '"><br>' : '';
                }
                $content = str_replace("{{other.approval_sign_sign_4}}", $li, $content);
            }

        $if_couplet=json_decode($data['couplet'],true)[0]['text']??"";
        $str='';
        if($if_couplet){
            $lian=['一','二','三','四','五'] ;
            for ($i = 0; $i < $data['number']; $i++) {
                $couplet=json_decode($data['couplet'],true)[$i]['text']??"";

                $str1 = '<div style="page-break-after: always;height:'.($data['page']*297).'mm;width:210mm;padding-right:40px;position:relative;">';

                for ($j = 0; $j < $data['page']; $j++) {
                 $top1=$j*297+148.5;
                 $str1 .= ' <p style="
                 -moz-writing-mode: vertical-rl;
                 -webkit-writing-mode: vertical-rl;
                 -ms-writing-mode: vertical-rl;
                 writing-mode: vertical-rl;
                  transform: translateY(-50%);
                  position: absolute;
                  right: 15px;
                  top:'.$top1.'mm;
                  font-size: 12px;
                  white-space: nowrap;
                  margin:0;"
                  >
                  ︙︙︙︙︙︙︙︙︙︙︙︙︙︙︙
                  第'.$lian[$i].'联：'.$couplet.'
                  ︙︙︙︙︙︙︙︙︙︙︙︙︙︙︙
                  </p>';
                  
                  $top1=$top1+100;
                }
                $str1.=$content.'</div>';
                $str= $str.$str1;
            }
        }else{
            for ($i = 0; $i < $data['number']; $i++) {
                $content= '<div style="page-break-after: always">'.$content.'</div>';
                $str= $str.$content;
            }
        }

            $content = ' <!DOCTYPE html>
            <html>
            <head>
            <meta http-equiv="content-type" content="text/html;charset=utf-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0" />
                <title>签约</title>
                <style>
                html,

                body {
                    margin: 0;
                }

                body{
                    padding: 0 40px;
                }

                @page{size: A4 portrait;margin:40px 0;}
              
                .border th{
                    border-right-width:1px;
                    border-right-color:inherit;
                    border-right-style:solid;
                }
                .border th:last-child{
                    border-right-width:none;
                    border-right-color:none;
                    border-right-style:none;
                }
                .border td{
                    border-right-width:1px;
                    border-right-color:inherit;
                    border-right-style:solid;
                    border-top-width:1px;
                    border-top-color:inherit;
                    border-top-style:solid;
                }
                .border td:last-child{
                    border-right-width:none;
                    border-right-color:none;
                    border-right-style:none;
                }
                </style>
            </head>
            <body>' . $str . '  </body>
            </html>';

            $da = [
                'content' => $content,
                'auto_num' => $auto_num ?? 0,
            ];

            return  $da;
        } catch (\Exception $e) {
            return showError($e->getLine());
            // return showError($e->getMessage());
            // return showError('协议模板存在非法参数');
        }
    }

    /**
     *数字金额转换成中文大写金额的函数
     *String Int $num 要转换的小写数字或小写字符串
     *return 大写字母
     *小数位为两位
     **/
    public function num_to_rmb($num) {
        if($num==0){
            return '零元';
        }
        $d = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); 
        $e = array('元', '拾', '佰', '仟', '万', '拾万', '佰万', '仟万', '亿', '拾亿', '佰亿', '仟亿'); 
        $p = array('分', '角'); 
        $zheng = "整"; 
        $final = array(); 
        $inwan = 0;//是否有万 
        $inyi = 0;//是否有亿 
        $len = 0;//小数点后的长度 
        $y = 0; 
        $num = round($num, 2);//精确到分 
        if(strlen($num) > 15){ 
        return "金额太大"; 
        die(); 
        } 
        if($c = strpos($num, '.')){//有小数点,$c为小数点前有几位 
        $len=strlen($num)-strpos($num,'.')-1;//小数点后有几位数 
        }else{//无小数点 
        $c = strlen($num); 
        $zheng = '整'; 
        } 
        for($i = 0; $i < $c; $i++){ 
        $bit_num = substr($num, $i, 1); 
        if ($bit_num != 0 || substr($num, $i + 1, 1) != 0) { 
         @$low = $low . $d[$bit_num]; 
        } 
        if ($bit_num || $i == $c - 1) { 
         @$low = $low . $e[$c - $i - 1]; 
        } 
        } 
        if($len!=1){ 
        for ($j = $len; $j >= 1; $j--) { 
         $point_num = substr($num, strlen($num) - $j, 1); 
         @$low = $low . $d[$point_num] . $p[$j - 1]; 
        } 
        }else{ 
        $point_num = substr($num, strlen($num) - $len, 1); 
        $low=$low.$d[$point_num].$p[$len]; 
        } 
        $chinses = str_split($low, 3);//字符串转化为数组 
        for ($x = count($chinses) - 1; $x >= 0; $x--) { 
        if ($inwan == 0 && $chinses[$x] == $e[4]) {//过滤重复的万 
         $final[$y++] = $chinses[$x]; 
         $inwan = 1; 
        } 
        if ($inyi == 0 && $chinses[$x] == $e[8]) {//过滤重复的亿 
         $final[$y++] = $chinses[$x]; 
         $inyi = 1; 
         $inwan = 0; 
        } 
        if ($chinses[$x] != $e[4] && $chinses[$x] !== $e[8]) { 
         $final[$y++] = $chinses[$x]; 
        } 
        } 
        $newstr = (array_reverse($final)); 
        $nstr = join($newstr); 
        if((substr($num, -2, 1) == '0') && (substr($num, -1) <> 0)){ 
        $nstr = substr($nstr, 0, (strlen($nstr) -6)).'零'. substr($nstr, -6, 6); 
        } 
        $nstr=(strpos($nstr,'零角')) ? substr_replace($nstr,"",strpos($nstr,'零角'),6) : $nstr; 
        return $nstr = (substr($nstr,-3,3)=='元') ? $nstr . $zheng : $nstr; 
       }
}
